import tool from 'src/util/tool'
import util from '_util/index';

const mutations = {
    add(state, n) {
        state.count += n
    },
    jian(state) {
        state.count--
    },
    setIsDetail(state, isDetail) {
        state.isDetail = isDetail
    },
    set_in_modal(state, in_modal) {
        state.in_modal = in_modal
    },
    setIslist(state, is_list) {
        state.is_list = is_list
    },
    set_modal_search_mid(state, modal_search_mid) {
        state.modal_search_mid = modal_search_mid
    },
    setNameAll(state, name_all) {
        state.name_all = name_all
    },
    setTopMid(state, top_mid) {
        state.top_mid = top_mid
    },
    setCurrent_names(state, current_names) {
        state.current_names = current_names
    },
    setCurrent_fields(state, current_fields) {
        state.current_fields = current_fields
    },
    setHardCodes(state, hardCodes) {
        state.hardCodes = hardCodes
    },
    set_editCom_fields(state, editCom_fields) {
        state.editCom_fields = editCom_fields
    },
    setBindVal(state, bindVal) {
        state.bindVal = bindVal
    },
    setSelectVals(state, selectVals) {
        state.selectVals = selectVals
    },
    setEditVo(state, vo) {
        if (Object.keys(vo).length == 0) {
            // state.edit.voErrors = {}
        }
        state.editVo = vo
    },
    setEditVoContact(state, vo) {
        if (Object.keys(vo).length == 0) {
            // state.edit.voErrors = {}
        }
        state.editVo = {...state.editVo, ...vo}
    },
    setMenuGroup(state, vo) {
        state.menuGroup = vo
    },
    setEditVoPop(state, vo) {
        state.editVoPop = vo
    },
    setEditVoModal(state, vo) {
        state.editVoModal = vo
    },
    setOtherEditVo(state, vo) {
        state.otherEditVo = vo
    },
    setOp(state, op) {
        state.op = op
    },
    setModalOp(state, modal_op) {
        state.modal_op = modal_op
    },
    set_otherEdit_op(state, otherEdit_op) {
        state.otherEdit_op = otherEdit_op
    },
    setEventVue(state, vue) {
        state.eventVue = vue
    },
    setCctl(state, current_ctl) {
        state.current_ctl = current_ctl
    },
    setSearchRs_cleared(state, searchRs_cleared) {
        state.searchRs_cleared = searchRs_cleared
    },
    setPopInputVal(state, popInputVal) {
        state.popInputVal = popInputVal
    },
    setSearchRs(state, rs) {
        state.searchRs = rs
        state.searchRs_cleared = false
    },
    setSearchLoader(state, b) {
        state.searchLoader = b
    },
    //顶级菜单的处理
    setMenus(state, menus) {
        state.menus = menus
    },
    setMenuAll(state, menus) {
        state.menu_all = menus
    },
    setModelAll(state, menus) {
        state.model_all = menus
    },
    setCurrentTopMenu(state) {
        // tool.cl(state.top_mid)
        let mid = state.top_mid
        if (!mid) {
            return;
        }
        let models = state.model_all || []
        let rsmodel = models.find(model => {
            return model.id == mid
        })
        if (!rsmodel) {
            return;
        }
        let ctl = rsmodel.name
        // tool.cl(ctl)
        let menus = state.menus
        for (let i = 0; i < menus.length; i++) {
            let menu = menus[i];
            if (menu.ctl == ctl) {
                state.current_top_menu = menu
            }
        }
    },
    upTopMid(state) {
        let menus = state.menu_all
        let ctl = state.current_ctl
        let rs;
        for (let i = 0; i < menus.length; i++) {
            let menu = menus[i];
            if (menu.ctl&&menu.ctl == ctl && menu.pid != 0) {
                rs = menu
                // tool.cl('menu:', menu);
                state.top_mid = menu.pid
                break;
            }
        }
    },
    getMenuByCtl(state, ctl) {
        let menus = state.menu_all
        let rs;
        for (let i = 0; i < menus.length; i++) {
            let menu = menus[i];
            if (menu.ctl == ctl && menu.pid != 0) {
                rs = menu
                break;
                // tool.cl(menu)
            }
        }
        state.vuexRs = rs
    },
    getModel(state, f) {
        f = f || 0
        let rs = 0;
        let models = state.model_all || []
        let ctl = state.current_ctl
        for (let i = 0; i < models.length; i++) {
            let model = models[i];
            if (model.name == ctl) {
                if (f == 1) {
                    rs = model
                }
                else
                    rs = model.id
            }
        }
        state.vuexRs = rs
    },
    getCtlByMid(state, mid) {
        let models = state.model_all || []
        let rsmodel = models.find(model => {
            return model.id == mid
        })
        state.vuexRs = rsmodel.name;
    },
    getModelByMid(state, mid) {
        let models = state.model_all || []
        let rsmodel = models.find(model => {
            return model.id == mid
        })
        state.vuexRs = rsmodel;
    },
    getModelByCtl(state, ctl) {
        let models = state.model_all || []
        let rsmodel = models.find(model => {
            return model.name == ctl
        })
        state.vuexRs = rsmodel;
    },
    setCache(state, prama) {
        let {
            key: key,
            val: val,
            group: group,
            exp: exp
        } = prama
        let name_all = state.name_all;

        exp = exp || 300;
        let expiration = new Date().getTime() + exp * 1000;

        if (group) {
            name_all = name_all[group] || {}
        }
        name_all[key] = val;
        name_all[key + '_exp'] = expiration;


        if (group)
            state.name_all[group] = name_all;
        else
            state.name_all = name_all;

    },
    getCache(state, prama) {
        let {
            key: key,
            group: group
        } = prama


        let exp = 0
        let name_all = {}
        if (group) {
            name_all = state.name_all[group]
        } else {
            name_all = state.name_all

        }
        if (!name_all) {
            state.cur_cache = null;
        }
        exp = name_all[key + '_exp'];
        if (new Date().getTime() < exp) {
        } else {
            delete name_all[key];
            if (group)
                state.name_all[group] = name_all;
            else
                state.name_all = name_all;
            state.cur_cache = null
        }
    },
    initUserCitys(state, payload){


        let rs = payload.list
        if(util.isArray(rs)) {

            let names = rs.map(function (item) {
                return item.region_name
            });
            let ids = rs.map(function (item) {
                return item.region_id
            });

            state.cityAll = rs.some(function (item) {
                return item.region_id == -1 ? true : false
            });

            state.cityNameLists = names.join(",");
            state.cityCodeLists = ids.join(",")

        }


    },
    setProductUnitVo(state, vo) {
        state.productUnitVo = vo
    },

}

export default mutations;
